package edu.iss.javaproject.service;

import java.util.Calendar;
import java.util.Date;

import org.springframework.stereotype.Service;

import edu.iss.javaproject.model.LeaveRecord;
import edu.iss.javaproject.model.Users;

@Service
public class LeaveServiceImpl implements LeaveService {

	LAPService lapservice;
	
	// Annual Leave Validation
	@Override
	public int validateAnnual(LeaveRecord leaveRecord){
		int current = leaveRecord.getUsers().getAnnual();
		int total = current + durationAnnual(leaveRecord);
		if(total <= 18){
			return total;
		} else {
			return -1;
		}
	}	
	
	
	@Override
	public int durationAnnual(LeaveRecord leaveRecord){
		Date startDate = leaveRecord.getLeaveStart();
		Date endDate = leaveRecord.getLeaveEnd();
		
		int diff = difference(leaveRecord);
		System.out.println(diff);
		double duration = diff;
		
		if(diff <= 14){
			Calendar cal1 = Calendar.getInstance();
			Calendar cal2 = Calendar.getInstance();
			cal1.setTime(startDate);
			cal2.setTime(endDate);
			
			int numberOfDays = 0;
			while (cal1.before(cal2)) {
				System.out.println("inloop");
				if ((Calendar.SATURDAY != cal1.get(Calendar.DAY_OF_WEEK))
					&&(Calendar.SUNDAY != cal1.get(Calendar.DAY_OF_WEEK))) {
					numberOfDays++;
					cal1.add(Calendar.DATE,1);
		        	}else {
		        		cal1.add(Calendar.DATE,1);
		        	}
			}
			duration = numberOfDays + 1;
			System.out.println(duration);
		} else if(diff <= 18){
			duration = diff;
		}
		return (int)duration;
	}


	//Medical Leave Validation
	@Override
	public int validateMedical(LeaveRecord leaveRecord){
		int current = leaveRecord.getUsers().getMedical();
		int total = current + difference(leaveRecord);
		if (total <= 60){
			return total;
		} else {
			return -1;
		}
	}
	
	@Override
	public int durationMedical(LeaveRecord leaveRecord){
		return difference(leaveRecord);
	}
	
	
	//Difference calculation for Annual/Medical
	private int difference(LeaveRecord leaveRecord){
		Date startDate = leaveRecord.getLeaveStart();
		Date endDate = leaveRecord.getLeaveEnd();
		
		long diff = endDate.getTime() - startDate.getTime();
		long diffDays = (diff / (24 * 60 * 60 * 1000)) + 1;

		return (int) diffDays;
	}
	
	
	// Compensation Leave Compensation
	@Override
	public boolean validateCompensation(LeaveRecord leaveRecord) {
		
		boolean result = false;
		double duration = leaveRecord.getDuration();
		System.out.println("duration:"+duration);
		System.out.println("diff:"+difference(leaveRecord));
		System.out.println("daysLeft:" +calCompensation(leaveRecord.getUsers()));
		
		if(duration <= calCompensation(leaveRecord.getUsers())){
			System.out.println("A:"+duration);
			System.out.println("B:"+difference(leaveRecord));
			System.out.println("C:"+(duration+0.5));
			if(duration == difference(leaveRecord) || (duration + 0.5) == difference(leaveRecord)){
				System.out.println("true");
				result = true;
			}
		} else {
			System.out.println("false");
			result = false;
		}
		System.out.println(result);
		return result;
	}
	
	@Override
	public double compensationDays(LeaveRecord leaveRecord){
		return leaveRecord.getUsers().getCompensation() + leaveRecord.getDuration();
	}

	private double calCompensation(Users user) {
		double daysLeft = Math.floor(user.getOtHours()*0.25)*0.5 - user.getCompensation();
		return daysLeft;	
	}	
	
	
}
